/**
 * 
 */
package org.intranet.lectorum.jdbctest;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.intranet.lectorum.hibernate.User;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

/**
 * @author Chris
 *
 */
public class JdbcTest {

	/**
	 * @throws java.lang.Exception
	 */
	@BeforeClass
	public static void setUpBeforeClass()
		throws Exception
	{
	}

	/**
	 * @throws java.lang.Exception
	 */
	@AfterClass
	public static void tearDownAfterClass()
		throws Exception
	{
	}

	/**
	 * @throws java.lang.Exception
	 */
	@Before
	public void setUp()
		throws Exception
	{
	}

	/**
	 * @throws java.lang.Exception
	 */
	@After
	public void tearDown()
		throws Exception
	{
	}

	@Test
	public void testJdbcConnection() throws SQLException
	{
		DriverManager.registerDriver(new org.gjt.mm.mysql.Driver());
		Connection conn = null;
		try {
			conn = DriverManager.getConnection("jdbc:mysql://localhost/lectorumdev?traceProtocol=false", "lectorum", "lectorum8");
			Statement s = conn.createStatement();
			ResultSet rs = s.executeQuery("select user_id, user_name, first_name, last_name from user");
			try {
				System.out.println("Rows="+rs.getFetchSize());
				while (rs.next())
				{
					System.out.print(rs.getInt(1));
					System.out.print(",");
					System.out.print(rs.getString(2));
					System.out.print(",");
					System.out.print(rs.getString(3));
					System.out.print(",");
					System.out.println(rs.getString(4));
				}
			} finally {
				if (rs != null) rs.close();
			}
			
		}
		finally {
			if (conn != null) conn.close();
		}
	}
	
	@Test
	public void testHibernate()
	{
		SessionFactory sessionFactory;

        Configuration configuration = new Configuration();
		Configuration configure = configuration.configure();
		sessionFactory = configure.buildSessionFactory();

		String username = "test7";

        Session session = sessionFactory.getCurrentSession();

        session.beginTransaction();

        User user = new User();
        user.setUserName(username);
        user.setFirstName("test");
        user.setLastName("four");
        user.setPassword("testpass2");

        session.save(user);

        Transaction transaction = session.getTransaction();
		transaction.commit();

		session = sessionFactory.getCurrentSession();
		session.beginTransaction();

		user = (User)session.createQuery("select u from User u where u.userName = :uname")
			.setParameter("uname", username).uniqueResult();
		System.out.println("userId="+user.getUserId());
		session.delete(user);
		session.getTransaction().commit();
	}
}
